package projects

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
	"xpms/app/models"
	"xpms/cores"
	"xpms/cores/utils"
)

type ProjectsTaskTime struct {
	Id                    int64 `orm:"pk;column(tasktimeid);" json:"Id,string"`
	Taskid                int64 `json:"Taskid,string"`
	Userid                int64 `json:"Userid,string"`
	Started               int64
	Ended                 int64
	Tasktime              string
	Surplustasktime       string
	Changed               int64
	Comment               string
	Created               int64
	Userrealname          string
	Cognizancetasktime    string
	Cognizanceuserid      int64
	Cognizanceusername    string
	Cognizanceusercomment string
	Cognizancetime        int64
}

type ProjectsTaskTimeAll struct {
	Id                    string ` son:"Id,string"`
	Taskid                int64  `json:"Taskid,string"`
	Taskname              string
	Userid                int64 `json:"Userid,string"`
	Userrealname          string
	Started               int64
	Ended                 int64
	Tasktime              string
	Surplustasktime       string
	Changed               int64
	Comment               string
	Created               int64
	Cognizancetasktime    string
	Cognizanceuserid      int64 `json:"Cognizanceuserid,string"`
	Cognizanceusername    string
	Cognizanceusercomment string
	Cognizancetime        int64
}

func (this *ProjectsTaskTime) TableName() string {
	return models.TableName("projects_task_time")
}
func init() {
	orm.RegisterModel(new(ProjectsTaskTime))
}

func AddTaskTime(upd ProjectsTaskTime) error {
	o := orm.NewOrm()
	upd.Changed = time.Now().Unix()
	upd.Created = time.Now().Unix()
	_, err := o.Insert(upd)
	return err
}

type TaskTimeDto struct {
	Id                    int64 `json:"Id,string"`
	Taskid                int64 `json:"Taskid,string"`
	Started               int64 `json:"Started,string"`
	Ended                 int64 `json:"Ended,string"`
	Tasktime              string
	Surplustasktime       string
	Comment               string
	Created               int64
	Cognizancetasktime    string
	Cognizanceusercomment string
	Cognizancetime        int64
	Type                  int // 0 代表任务本人 1 代表认定工时
}

/**
批量保存
*/
func AddAndUpdateMultiTaskTime(userid int64, userName string, upd []TaskTimeDto) {
	o := orm.NewOrm()
	to, err := o.Begin()
	for _, v := range upd {
		tasktime := new(ProjectsTaskTime)
		var col []string
		tasktime.Changed = time.Now().Unix()
		col = append(col, "changed")
		if !utils.IsEmpty(v.Taskid) {
			tasktime.Taskid = v.Taskid
			col = append(col, "taskid")
		}
		if !utils.IsEmpty(v.Tasktime) {
			tasktime.Tasktime = v.Tasktime
			col = append(col, "tasktime")
		}
		if !utils.IsEmpty(v.Surplustasktime) {
			tasktime.Surplustasktime = v.Surplustasktime
			col = append(col, "surplustasktime")
		}
		if !utils.IsEmpty(v.Comment) {
			tasktime.Comment = v.Comment
			col = append(col, "comment")
		}
		if !utils.IsEmpty(v.Started) {
			tasktime.Started = v.Started
			col = append(col, "started")
		}
		if !utils.IsEmpty(v.Ended) {
			tasktime.Ended = v.Ended
			col = append(col, "ended")
		}
		if v.Id > 0 {
			tasktime.Id = v.Id
			if !utils.IsEmpty(v.Cognizancetasktime) {
				tasktime.Cognizancetasktime = v.Cognizancetasktime
				col = append(col, "cognizancetasktime")
			}
			if !utils.IsEmpty(v.Cognizanceusercomment) {
				tasktime.Cognizanceusercomment = v.Cognizanceusercomment
				col = append(col, "cognizanceusercomment")
			}
			if v.Type != 0 {
				tasktime.Cognizanceuserid = userid
				tasktime.Cognizanceusername = userName
				tasktime.Cognizancetime = time.Now().Unix()
				col = append(col, "cognizanceuserid")
				col = append(col, "cognizanceusername")
				col = append(col, "cognizancetime")
			}
			_, err = to.Update(tasktime, utils.RemoveDuplicatesAndEmpty(col)...)
		} else {
			idn := utils.SnowFlakeId()
			tasktime.Id = idn
			tasktime.Created = time.Now().Unix()
			tasktime.Userid = userid
			tasktime.Userrealname = userName
			_, err = to.Insert(tasktime)
		}
		if err != nil {
			to.Rollback()
			break
		}
	}
	if err != nil {
		to.Rollback()
	} else {
		to.Commit()
	}
}

func TaskTimeExec(condArr map[string]interface{}, page int, offset int) (ops []ProjectsTaskTimeAll) {
	o := orm.NewOrm()
	var task []ProjectsTaskTimeAll
	o.Raw(tasktimeSql(condArr)).QueryRows(&task)
	return task
}

/**
  组装sql
*/
func tasktimeSql(condArr map[string]interface{}) (string, []interface{}) {
	var sql string
	sql = `
	SELECT
      t.tasktimeid id,
      t.*
  FROM pms_projects_task_time t
	WHERE 1 = 1 `
	param := make([]interface{}, 0, 0)
	if !utils.IsEmpty(condArr["taskid"]) {
		sql = sql + ` and  t.taskid in(`
		sql1, value := cores.SqlInParam(condArr["taskid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["userid"]) {
		sql = sql + ` and  t.userid in(`
		sql1, value := cores.SqlInParam(condArr["userid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	return sql, param
}
